(function () {

    "use strict";

    var api = {
        getListsUrl: "/api/ticket/getTicketList",
        getTicketUrl: "/api/ticket/getTicket"
    };

    sm.controller('wx-center-ticket-get-controller', ['$scope', '$http', 'Toast', function ($scope, $http, Toast){

        $scope.selected = false;
        $scope.lists = [];
        $scope.getLists = function () {
            $http.get(api.getListsUrl, {
                params: {
                    page: 1,
                    type: "noget"
                }
            }).success(function (res) {
                $scope.selected = true;
                if (res.state) {
                    $scope.lists = res.data
                }
            });

        };

        $scope.getLists();


        $scope.clickToGetTicket = function (index) {

            if ($scope.lists.length <= 0) {
                return false;
            }

            var id = $scope.lists[index].id;
            if (!id) {
                return false;
            }

            $http.post(api.getTicketUrl, {
                id: id
            }).success(function (res) {
                if (res.state) {
                    $scope.lists.splice(index, 1)
                    Toast.success({title: "领取成功"});
                } else {
                    Toast.error({title: "领取失败"})
                }
            });
        }


    }]);
})();

